﻿using System;
using System.Drawing;

namespace DotNet.Common
{
    public class CommonControl
    {
        int m_left, m_top, m_width, m_height;
        Pen m_forePen;
        Font m_foreFont;
        Brush m_foreBrush;

        public int Left
        {
            get { return m_left; }
            set { m_left = value; }
        }

        public int Top
        {
            get { return m_top; }
            set { m_top = value; }
        }

        public int Width
        {
            get { return m_width; }
            set { m_width = value; }
        }

        public int Height
        {
            get { return m_height; }
            set { m_height = value; }
        }

        public Pen ForePen
        {
            get { return m_forePen; }
            set { m_forePen = value; }
        }

        public Font ForeFont
        {
            get { return m_foreFont; }
            set
            {
                m_foreFont = value;
            }
        }

        public Brush ForeBrush
        {
            get { return m_foreBrush; }
            set { m_foreBrush = value; }
        }

        public event VoidInvoker OnResize;

        public CommonControl()
        {
            m_forePen = new Pen(Color.White);
        }

        public static int GetStringWidth(IntPtr handle, string str, Font font)
        {
            using (Graphics g = Graphics.FromHwnd(handle))
            {
                return (int)Math.Round(g.MeasureString(str, font).Width);
            }
        }

        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void Resize(int left, int top, int width, int height)
        {
            if (left < 0 || top < 0 || width < 0 || height < 0)
                return;

            m_left = left;
            m_top = top;
            m_width = width;
            m_height = height;

            if (OnResize != null)
            {
                OnResize();
            }
        }
    }
}
